package com.ramos.kuaixiao.ui;

import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.ramos.kuaixiao.App;
import com.ramos.kuaixiao.R;
import com.ramos.kuaixiao.db.AllCustomerDB;
import com.ramos.kuaixiao.db.AllCustomerStoreDB;
import com.ramos.kuaixiao.db.BrandDB;
import com.ramos.kuaixiao.db.BrandInfoDB;
import com.ramos.kuaixiao.db.BrandKeywordsDB;
import com.ramos.kuaixiao.db.ClientPriceDB;
import com.ramos.kuaixiao.db.CustomerDB;
import com.ramos.kuaixiao.db.CustomerStoreDB;
import com.ramos.kuaixiao.db.CustomerWareHouseDB;
import com.ramos.kuaixiao.db.DBHelper;
import com.ramos.kuaixiao.db.EmpBrandDB;
import com.ramos.kuaixiao.db.EmpStoreHouseDB;
import com.ramos.kuaixiao.db.OrderDB;
import com.ramos.kuaixiao.db.PriceDB;
import com.ramos.kuaixiao.db.PriceTypeDB;
import com.ramos.kuaixiao.db.ProductTypeDB;
import com.ramos.kuaixiao.db.SaleCustomerDB;
import com.ramos.kuaixiao.db.SaleCustomerStoreDB;
import com.ramos.kuaixiao.db.ShopsDB;
import com.ramos.kuaixiao.db.StoreNumDB;
import com.ramos.kuaixiao.db.UnSubmitOrderDB;
import com.ramos.kuaixiao.db.WareHouseDB;
import com.ramos.kuaixiao.driver.ui.DriverMainActivity;
import com.ramos.kuaixiao.shops.ui.OrderMainActivity;
import com.ramos.kuaixiao.utils.SharedPreference;
import com.ramos.kuaixiao.utils.StringUtil;
import com.ramos.kuaixiao.utils.Utils;
import com.tencent.stat.MtaSDkException;
import com.tencent.stat.StatService;


public class SplashActivity extends Activity {
    private static final int SPLASH_LOGO_INTERVAL = 2 * 1000;
    TextView tvVersion;
    int num=0;
    private Handler mHandler = new Handler();
    private Runnable mDelayRunnable = new Runnable() {
        @Override
        public void run() {
            num++;
            goNextPage();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        View decorView = getWindow().getDecorView();
        int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_FULLSCREEN;
        decorView.setSystemUiVisibility(uiOptions);
        mHandler.postDelayed(mDelayRunnable, SPLASH_LOGO_INTERVAL);
        tvVersion = findViewById(R.id.tv_version);
        tvVersion.setText("当前版本号:"+Utils.getVersionName(this));


        // 因此，MTA的初始化工作需要在本onCreate中进行
// 在startStatService之前调用StatConfig配置类接口，使得MTA配置及时生
        //String appkey = "amtaandroid0";
// 初始化并启动MTA
       try {
// 第三个参数必须为：com.tencent.stat.common.StatConstants.VERSION
            StatService.startStatService(this, "ADLAE8G39Y8E",
                    com.tencent.stat.common.StatConstants.VERSION);
            Log.d("MTA","MTA初始化成功");
        } catch (MtaSDkException e) {
// MTA初始化失败
            Log.d("MTA","MTA初始化失败"+e);
        }
    }

    private void goNextPage() {
        if (isFinishing()) {
            return;
        }
        Log.d("onUpgrade", "SplashActivity,App.isOldVersion:"+ App.isOldVersion);
        if(Utils.getVersion(this) == 6 && !SharedPreference.hasPreferString("isUpdate")){
            if(!StringUtil.isEmpty(SharedPreference.getPreferString("password"))){
                clearDB();
                startActivity(new Intent(SplashActivity.this, LoadingActivity.class));
            } else {
                startActivity(new Intent(SplashActivity.this, LoginActivity.class));
            }

        } else {
            if (!StringUtil.isEmpty(SharedPreference.getPreferString("password"))) {
                //SharedPreference.setPreferInteger("isFirstLogin", 1);
                if (!SharedPreference.hasPreferString("isOldVersion")) {
                    startActivity(new Intent(SplashActivity.this, LoadingActivity.class));
                } else {
                    if (SharedPreference.getPreferString("type").equals("10")) {
                        startActivity(new Intent(SplashActivity.this, MainActivity.class));
                        finish();
                    } else if (SharedPreference.getPreferString("type").equals("20")) {
                        startActivity(new Intent(SplashActivity.this, OrderMainActivity.class));
                        finish();
                    } else if (SharedPreference.getPreferString("type").equals("11")) {
                        startActivity(new Intent(SplashActivity.this, DriverMainActivity.class));
                        finish();
                    }
                }
            }else {
                startActivity(new Intent(SplashActivity.this, LoginActivity.class));
            }
        }
    }

    private void clearDB() {
        SQLiteDatabase db = DBHelper.getInstance().getReadableDatabase();
        db.execSQL(BrandInfoDB.DROP_USERINFO);
        db.execSQL(BrandInfoDB.CREATE_TABLE_SQL);
        db.execSQL(WareHouseDB.DROP_USERINFO);
        db.execSQL(WareHouseDB.CREATE_TABLE_SQL);
        db.execSQL(CustomerDB.DROP_USERINFO);
        db.execSQL(CustomerDB.CREATE_TABLE_SQL);
        db.execSQL(CustomerStoreDB.DROP_USERINFO);
        db.execSQL(CustomerStoreDB.CREATE_TABLE_SQL);
        db.execSQL(AllCustomerDB.DROP_USERINFO);
        db.execSQL(AllCustomerDB.CREATE_TABLE_SQL);
        db.execSQL(AllCustomerStoreDB.DROP_USERINFO);
        db.execSQL(AllCustomerStoreDB.CREATE_TABLE_SQL);
        db.execSQL(BrandDB.DROP_USERINFO);
        db.execSQL(BrandDB.CREATE_TABLE_SQL);
        db.execSQL(ShopsDB.DROP_USERINFO);
        db.execSQL(ShopsDB.CREATE_TABLE_SQL);
        db.execSQL(StoreNumDB.DROP_USERINFO);
        db.execSQL(StoreNumDB.CREATE_TABLE_SQL);
        db.execSQL(PriceDB.DROP_USERINFO);
        db.execSQL(PriceDB.CREATE_TABLE_SQL);
        db.execSQL(CustomerWareHouseDB.DROP_USERINFO);
        db.execSQL(CustomerWareHouseDB.CREATE_TABLE_SQL);
        //db.execSQL(OrderDB.CREATE_TABLE_SQL);
        db.execSQL(EmpBrandDB.DROP_USERINFO);
        db.execSQL(EmpBrandDB.CREATE_TABLE_SQL);
        db.execSQL(PriceTypeDB.DROP_USERINFO);
        db.execSQL(PriceTypeDB.CREATE_TABLE_SQL);
        db.execSQL(ClientPriceDB.DROP_USERINFO);
        db.execSQL(ClientPriceDB.CREATE_TABLE_SQL);
        db.execSQL(EmpStoreHouseDB.DROP_USERINFO);
        db.execSQL(EmpStoreHouseDB.CREATE_TABLE_SQL);
        db.execSQL(ProductTypeDB.DROP_USERINFO);
        db.execSQL(SaleCustomerDB.DROP_USERINFO);
        db.execSQL(SaleCustomerDB.CREATE_TABLE_SQL);
        db.execSQL(SaleCustomerStoreDB.DROP_USERINFO);
        db.execSQL(SaleCustomerStoreDB.CREATE_TABLE_SQL);
        db.execSQL(BrandKeywordsDB.DROP_USERINFO);
        db.execSQL(BrandKeywordsDB.CREATE_TABLE_SQL);


        new AllCustomerDB().deleteAll();
        new AllCustomerStoreDB().deleteAll();
        new BrandDB().deleteAll();
        new BrandInfoDB().clearShopMall();
        new ClientPriceDB().deleteAll();
        new CustomerDB().deleteAll();
        new CustomerStoreDB().deleteAll();
        new CustomerWareHouseDB().deleteAll();
        new EmpBrandDB().deleteAll();
        new EmpStoreHouseDB().deleteAll();
        new OrderDB().deleteAll();
        new PriceDB().deleteAll();
        new PriceTypeDB().deleteAll();
        new SaleCustomerDB().deleteAll();
        new SaleCustomerStoreDB().deleteAll();
        new ShopsDB().deleteAll();
        new StoreNumDB().deleteAll();
        new UnSubmitOrderDB().deleteAll();
        new WareHouseDB().deleteAll();
    }
}
